<?php
/**
 * 角色管理
 */
namespace app\admins\controller\sys;
use \app\admins\controller\Auth;
class Role extends Auth{
    
    
    /**
     * 角色列表
     */
    public function index(){
        $limit = 20;
        $page  = input('page');
        $keyword = input('keyword');        
        $param = [
            'page'  => $page,
            'limit' => $limit,
            'role'  => $keyword,      
            'plat'  => $this->plat,  
        ];        
        $Role = new \logic\sys\power\Role($this->getUser());
        $data = $Role->getList($param);
        $platlist = config('servces.site_plat_list');
        $showpage = showpage($data['total'],$page,$data['limit']);
        $this->assign(get_defined_vars());
        return $this->fetch();
    }
    
    /**
     * 添加角色
     */
    public function add(){        
        if( $_POST ){
            try{                
                $param = array(
                    'plat'      => input('plat'),
                    'role'      => input('role'),
                    'remark'    => input('remark'),
                );
                $Role = new \logic\sys\power\Role($this->getUser());
                $result = $Role->add($param);
                return $this->success('添加成功','/sys/role/index');
            }catch( \think\Exception $e){
                $this->error($e->getMessage());
            }                
        }
        $platlist = config('servces.site_plat_list');
        $this->assign(get_defined_vars());
        return $this->fetch();
    }
    
    /**
     * 修改角色
     */
    public function edit(){
        try{  
            $id = input('id');            
            $Role = new \logic\sys\power\Role($this->getUser());
            if( $_POST ){                              
                $param = array(
                    'plat'      => input('plat'),
                    'role'      => input('role'),
                    'remark'    => input('remark'),
                );                
                $result = $Role->edit($id,$param);
                return $this->success('修改成功','/sys/role/index');     
                exit;                           
            }
            $RoleMenu = new \logic\sys\power\RoleMenu($this->getUser());
            $checkpower = $RoleMenu->getRolePowerList($id);
            $info =  $Role->find(['id'=>input('id')]);
            $platlist = config('servces.site_plat_list');
            $this->assign(get_defined_vars());
            return $this->fetch();
            
        }catch( \think\Exception $e){
            $this->error($e->getMessage());
        }
    }
    
    /**
     * 更新角色状态
     */
    public function del(){
        
    }
    
    
    /**
     * 授权
     */
    public function power(){        
        try{
            $roleid = input('id');
            $Role = new \logic\sys\power\Role($this->getUser());
            $roleinfo = $Role->find(['id'=>$roleid]);
            if($roleinfo['type'] != 2 )  E('请勿非法操作');
                        
            if(request()->isPost()){
                $code = input('nodes');        
                $code = explode(',',$code);
                $RoleMenu = new \logic\sys\power\RoleMenu($this->getUser());
                $result = $RoleMenu->editPower($roleid,$code);
                return $this->success('成功修改权限','/sys/role/index');
            }
            
            //所有的节点
            $Menu = new \logic\sys\power\Menu($this->getUser());
            $menuListAll = $Menu->getList(['plat'=>$roleinfo['plat']]); 
            // 角色拥有的权限
            $RoleMenu = new \logic\sys\power\RoleMenu($this->getUser());
            
            $menuids = $RoleMenu->getList($roleid);            
            $menulist = nodeMerge($menuListAll['list']);
            $menulist = $this->filterMenu($menulist);   
            $this->assign(get_defined_vars());
            return $this->fetch();
        }catch(Exception $e){
            $this->error($e->getMessage());
        }
    }
    
    private function filterMenu($menulist){
        return $menulist;
    }
    
    
    
    
}